import BaseUtil from '@/BaseUtil';
import BuffUtil from '@/BuffUtil';
import MathUtil from '@/MathUtil';
import RandomUtil from '@/RandomUtil';
import SelectUtil from '@/SelectUtil';
import { OrderId } from '@/globals/order';
import BuffCommon from 'code/Core/Common/BuffCommon';
import MathCommon from 'code/Core/Common/MathCommon';
import MovementCommon from 'code/Core/Common/MovementCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 奥术飞弹
 **/
export default class FS_ArcanMissile extends AbilityBase {
    static instance: FS_ArcanMissile;
    buffType: AppBuffType = {
        id: 'buff_FS_ArcanMissile',
        art: this.art,
        name: this.abilityName,
        ubertip: '减速中',
        attribute: {
            move_speed: -10,
            命中: -0.05,
        },
        dur: 3,
        stackCountMax: -1,
    };
    constructor(id: string) {
        super(id);
        FS_ArcanMissile.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let lv = this.getAbilityLevel(source);
        let count = MathCommon.getAbilityEffectCount(3 + lv * 2, source, this.id);
        let interval = 3 / count;
        this.hit(source, tarX, tarY);
        BaseUtil.onTimer(interval, (c) => {
            let cmd = GetUnitCurrentOrder(source);
            if (cmd != OrderId.Spellstealon) return false;
            this.hit(source, tarX, tarY);
            return c < count - 1;
        });
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    hit(source: unit, x: number, y: number) {
        let { x: tx, y: ty } = MathUtil.polarProjection(x, y, RandomUtil.nextInt(0, 175), RandomUtil.randomAngle());
        MovementCommon.Sin曲线投射物({
            missile: 'Abilities\\Weapons\\DragonHawkMissile\\DragonHawkMissile.mdl',
            tarX: tx,
            tarY: ty,
            speed: 800,
            caster: source,
            onEnd: this.onMissielReach.bind(this),
            maxHeight: RandomUtil.nextInt(150, 400),
            radius: RandomUtil.nextInt(200, 300),
        });
    }
    onMissielReach(p: 曲线投射物参数) {
        DestroyEffect(AddSpecialEffect('Abilities\\Spells\\NightElf\\Blink\\BlinkCaster.mdl', p.curX, p.curY));
        let es = SelectUtil.getEnemyUnitsInRange(p.caster, 100, p.curX, p.curY);
        let lv = this.getAbilityLevel(p.caster);
        let dmg = 20 * lv + UnitCommon.取法强(p.caster) * (0.3 + 0.4 * lv);
        for (let e of es) {
            // BuffUtil.addBuff(e, this.buffType.id, p.caster);
            BuffCommon.addBuff({ target: e, caster: p.caster, buffTypeId: this.buffType.id, ablityId: this.id });
            DamageSystemInstance.applyArcaneDamage(e, p.caster, dmg, this.id);
        }
    }
}
